package com.zy.reggie.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.reggie.common.R;
import com.zy.reggie.dto.DishDto;
import com.zy.reggie.entity.Dish;
import com.zy.reggie.entity.DishFlavor;
import com.zy.reggie.mapper.DishMapper;
import com.zy.reggie.service.DishFlavorService;
import com.zy.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    //同时操作两张表，所以要加事务，记得在启动类中开启事务
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //首先保存菜品的基本数据，但不包括口味数据
        this.save(dishDto);
        //得到对应菜品的id
        Long dishId = dishDto.getId();
        //得到所有的口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历集合flavors,将菜品的ID与之其自己的口味相对应
        flavors = flavors.stream().map((item)->{
            //id赋值
            item.setDishId(dishId);
                return item;
        }).collect(Collectors.toList());
        //保存到dish_flavor数据表中
        dishFlavorService.saveBatch(flavors);

    }

    /**
     *根据id查询对应的菜品信息及口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //获取dish表中的数据
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //获取dish_flavor表中的数据
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 更新菜品信息，同时更新对应的口味信息
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表中的数据
        this.updateById(dishDto);

        //更新dish_flavor表中的数据
        //1.先清除表中的数据
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);
        //2.往表中插入数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //注意此时的flavors只有数据，并没有对应的菜品ID
        //遍历集合flavors,将菜品的ID与之其自己的口味相对应
        flavors = flavors.stream().map((item)->{
            //id赋值
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存到dish_flavor数据表中
        dishFlavorService.saveBatch(flavors);

    }
}
